/* A simple server in the internet domain using TCP
   The port number is passed as an argument */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>

pthread_t tid[2];
int vet1[100], vet2[100];


void error(const char *msg)
{
    perror(msg);
    exit(1);
}


void* server_receve1()
{     int sockfd, newsockfd,portno;
     socklen_t clilen;
     char buffer[256];
	int i=0;
     struct sockaddr_in serv_addr, cli_addr;
     int n;


     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
	portno = 1235;
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);

     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,1);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
	i=0;
    
	bzero(buffer,256);     
	while(i<100){ 
     n = read(newsockfd,buffer,255);
     if (n < 0) error("ERROR reading from socket");

	vet2[i]=atoi(buffer);
	printf("%d:   %d \n",i,vet2[i]);
	i++;
	}
	close(newsockfd);
   	close(sockfd);
	pthread_exit(NULL);

	
	}

void* server_receve()
{
     int sockfd, newsockfd,portno;
     socklen_t clilen;
     char buffer[256];
	int i=0;
     struct sockaddr_in serv_addr, cli_addr;
     int n;
	pthread_create(&(tid[1]), NULL, &server_receve1, NULL);

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
	portno = 1234;
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);

     if (bind(sockfd, (struct sockaddr *) &serv_addr,
              sizeof(serv_addr)) < 0) 
              error("ERROR on binding");
     listen(sockfd,1);
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd, 
                 (struct sockaddr *) &cli_addr, 
                 &clilen);
     if (newsockfd < 0) 
          error("ERROR on accept");
	i=0;
    
	bzero(buffer,256);     
	while(i<100){ 
     n = read(newsockfd,buffer,255);
     if (n < 0) error("ERROR reading from socket");

	vet1[i]=atoi(buffer);
	printf("%d:   %d \n",i,vet1[i]);
i++;
}
close(newsockfd);
    close(sockfd);

	pthread_join(tid[1],NULL);
	pthread_exit(NULL);
	
}
int main(int argc, char *argv[])
{
	int i;


	
	pthread_create(&(tid[0]), NULL, &server_receve, NULL);
		
	pthread_join(tid[0],NULL);
	printf("vetor1:\n");
	for (i=0;i<100;i++)
		printf("%d\n",vet1[i]);
	
	printf("vetor2:\n");
	for (i=0;i<100;i++)
		printf("%d\n",vet2[i]);
	
	     return 0; 
}
